package com.yuneec.fly.presenters;

import android.os.SystemClock;
import android.util.Log;

import com.yuneec.droneservice.ServiceManager;
import com.yuneec.droneservice.command.BindAircraft;
import com.yuneec.droneservice.command.CommandListener;
import com.yuneec.droneservice.command.GetAircraftInfo;
import com.yuneec.droneservice.command.MAV_RC_ERROR_CODE;
import com.yuneec.droneservice.command.ScanAircraft;
import com.yuneec.droneservice.command.UnbindAircraft;
import com.yuneec.droneservice.model.Aircraft;
import com.yuneec.droneservice.request.IResponse;

import java.util.ArrayList;
import java.util.List;

public class DroneStatePresenter {
    //当前的操作
    public volatile int actor;
    public static final int NONE = 10;
    public static final int DO_SCANING = 11;
    public static final int DO_BINDING = 12;


    private byte lock[] = new byte[0];

    public final Aircraft getCurrentBindInfo() {
        final Aircraft[] aircraft = {null};
        final boolean[] getResponse = new boolean[1];
        ServiceManager.getInstance().sendDroneCommand(new GetAircraftInfo(), new CommandListener<Aircraft>() {
            @Override
            public void onSuccess(IResponse<Aircraft> response) {
                aircraft[0] = response.getParam();
                getResponse[0] = true;
            }

            @Override
            public void onError(int errorCode) {
                getResponse[0] = true;
            }
        });
        for (int i = 0; i < 100; i++) {
            if (getResponse[0]) {
                break;
            }
            SystemClock.sleep(200);
        }
        return aircraft[0];
    }


    public final List<Aircraft> droneScan() {
        final List<Aircraft>[] res = new ArrayList[]{null};
        if (actor != DO_SCANING) {
            synchronized (lock) {
                if (actor != DO_SCANING) {
                    actor = DO_SCANING;
                    ServiceManager.getInstance().sendDroneCommand(new ScanAircraft(), new CommandListener<Aircraft[]>() {
                        @Override
                        public void onSuccess(IResponse<Aircraft[]> response) {
                            res[0] = new ArrayList<Aircraft>();
                            Aircraft[] aircrafts = response.getParam();
                            if (aircrafts != null) {
                                for (Aircraft aircraft : aircrafts) {
                                    res[0].add(aircraft);
                                }
                            }
                        }

                        @Override
                        public void onTimeout() {
                            res[0] = new ArrayList<Aircraft>();
                            super.onTimeout();
                        }

                        @Override
                        public void onError(int errorCode) {
                            res[0] = new ArrayList<Aircraft>();
                            super.onError(errorCode);
                        }

                    });

                    for (int i = 0; i < 100; i++) {
                        if (res[0] != null) {
                            break;
                        }
                        SystemClock.sleep(200);
                    }

                    actor = NONE;
                }
            }
        }
        return res[0];
    }

    public final boolean droneBind(int nodeId) {
        if (nodeId == -1) {
            return false;
        }
        if (actor != DO_BINDING) {
            synchronized (lock) {
                if (actor != DO_BINDING) {
                    actor = DO_BINDING;
                    final boolean[] getResponse = {false};
                    final boolean[] result = {false};
                    ServiceManager.getInstance().sendDroneCommand(new BindAircraft(nodeId), new CommandListener() {
                        @Override
                        public void onSuccess(IResponse response) {
                            Log.d("zzd_aoa", "bind_air success!!!");
                            result[0] = response.getCode() == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS;
                            getResponse[0] = true;
                        }

                        @Override
                        public void onError(int errorCode) {
                            Log.d("zzd_aoa", "bind_air err:" + errorCode);
                            getResponse[0] = true;
                        }

                        @Override
                        public void onTimeout() {
                            Log.d("zzd_aoa", "bind_air timeout");
                            getResponse[0] = true;
                        }

                    });
                    for (int i = 0; i < 100; i++) {
                        if (getResponse[0]) {
                            break;
                        }
                        SystemClock.sleep(200);
                    }
                    actor = NONE;
                    return result[0];
                }
            }
        }
        return false;
    }


    public final void droneUnBind() {
        if (actor != DO_BINDING) {
            synchronized (lock) {
                if (actor != DO_BINDING) {
                    actor = DO_BINDING;
                    ServiceManager.getInstance().sendDroneCommand(new UnbindAircraft(), null);
                    actor = NONE;
                }
            }
        }
    }

}
